package com.wwtech.epp.ui.activity.work;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.socks.library.KLog;
import com.wwtech.epp.R;
import com.wwtech.epp.application.AppConfig;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.entity.Daily;
import com.wwtech.epp.ui.activity.base.MyBaseFragment;
import com.wwtech.epp.ui.activity.work.component.DaggerDailyComponent;
import com.wwtech.epp.ui.activity.work.contract.DailyContract;
import com.wwtech.epp.ui.activity.work.module.DailyModule;
import com.wwtech.epp.ui.activity.work.presenter.DailyPresenter;
import com.wwtech.epp.ui.adapter.work.DailyListAdapter;
import com.wwtech.epp.utils.ToastUtil;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.work
 * @Description: $description
 * @date 2017/12/07 17:36:20
 */

public class DailyFragment extends MyBaseFragment implements DailyContract.View ,SwipeRefreshLayout.OnRefreshListener,BaseQuickAdapter.RequestLoadMoreListener {

    @Inject
    DailyPresenter mPresenter;
    @Inject
    DailyListAdapter dailyListAdapter;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    private String mType;
    private int mNextPage;
    private static final int ONE_PAGE_SIZE = 5;
    private int total;
    private static final String ARG_TYPE = "arg_type";

   @Nullable
   @Override
   public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_daily, null);
       ButterKnife.bind(this, view);
       Bundle mBundle = getArguments();
       refreshLayout.setOnRefreshListener(this);
       recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
       recyclerView.setAdapter(dailyListAdapter);
       dailyListAdapter.setOnLoadMoreListener(this, recyclerView);
       dailyListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
           @Override
           public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
               Intent intent = new Intent(getActivity(), WebViewActivity.class);
               intent.putExtra("url", dailyListAdapter.getData().get(i).getActionUrl());
               KLog.i(dailyListAdapter.getData().get(i).getActionUrl());
               if (dailyListAdapter.getData().get(i).getActionUrl() == null || dailyListAdapter.getData().get(i).getActionUrl().equals("")) {
                   ToastUtil.displayShortToast("数据异常，请退出该页面重新进入");
                   return;
               }
               if ("我发出的".equals(mType)) {
                   intent.putExtra("title", ContainValue.userLogin.getData().getNickName() + "的" + dailyListAdapter.getData().get(i).getReportType() + "详情");
               } else {
                   intent.putExtra("title",  dailyListAdapter.getData().get(i).getSendUserName() + "的" + dailyListAdapter.getData().get(i).getReportType() + "详情");
               }
               startActivity(intent);
           }
        });
       return view;
   }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mType = getArguments().getString(ARG_TYPE);
            dailyListAdapter.setType(mType);
        }
    }
    @Override
    public void fetchData() {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                loadDataFromServer(true);
            }
        });
    }

    public void loadDataFromServer(boolean showRefresh) {
        refreshLayout.setRefreshing(showRefresh);
        if (showRefresh) {
            mNextPage = 1;
        }
        mPresenter.getDailyListFromServer(mType, mNextPage, ONE_PAGE_SIZE);
    }

    public static DailyFragment newInstance(String param) {
        DailyFragment fragment = new DailyFragment();
        Bundle args = new Bundle();
        args.putString(ARG_TYPE, param);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onRefresh() {
        mNextPage = 1;
        loadDataFromServer(true);
    }

    @Override
    public void onLoadMoreRequested() {
        if (dailyListAdapter.getData().size() < ONE_PAGE_SIZE || dailyListAdapter.getData().size() >= total) {
            dailyListAdapter.loadMoreEnd(true);
            return;
        }
        mNextPage ++;
        mPresenter.getDailyListFromServer(mType, mNextPage, ONE_PAGE_SIZE);
    }


    @Override
    protected void setupFragmentComponent() {
       DaggerDailyComponent
               .builder()
               .appComponent(((AppConfig) getActivity().getApplication()).getApplicationComponent())
               .dailyModule(new DailyModule(this))
               .build()
               .inject(this);
    }
    @Override
    public void setPresenter(DailyContract.DailyContractPresenter presenter) {
        mPresenter = (DailyPresenter) presenter;
    }

    @Override
    protected void initDataFromLocal() {

    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

    @Override
    public void setListData(Daily taskList) {
        total = taskList.getData().getTotal();
        if (mNextPage == 1) {
            refreshLayout.setRefreshing(false);
            dailyListAdapter.setNewData(taskList.getData().getList());
        } else {
            dailyListAdapter.loadMoreComplete();
            dailyListAdapter.addData(taskList.getData().getList());
        }
    }

}